package ren.xxpure.service.impl;

import cn.hutool.http.HttpStatus;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import ren.xxpure.dao.TodoRepository;
import ren.xxpure.entity.CommonResult;
import ren.xxpure.entity.LoginUser;
import ren.xxpure.entity.Todo;
import ren.xxpure.service.TodoService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class TodoServiceImpl implements TodoService {
	@Resource
	private
	MongoTemplate mongoTemplate;
	@Resource
	private TodoRepository todoRepository;

	@Override
	public CommonResult<List<Todo>> selectList(Todo todo) {
		LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		todo.setUid(principal.getUsername());
		Query query = new Query();
		if (!StringUtils.isEmpty(todo.getId())) {
			query.addCriteria(Criteria.where("id").is(todo.getId()));
		}
		if (!StringUtils.isEmpty(todo.getUid())) {
			query.addCriteria(Criteria.where("uid").is(todo.getUid()));

		}
		if (!StringUtils.isEmpty(todo.getContent())) {
			query.addCriteria(Criteria.where("content").regex(todo.getContent()));
		}
		if (todo.getIsFinish() != null) {
			query.addCriteria(Criteria.where("isFinish").is(todo.getId()));
		}
		if (todo.getTime() != null) {
			query.addCriteria(Criteria.where("time").is(todo.getTime()));
		}
		if (todo.getRepeat() != null) {
			query.addCriteria(Criteria.where("repeat").is(todo.getRepeat()));
		}
		if (todo.getRepeatBin() != null) {
			query.addCriteria(Criteria.where("repeatBin").is(todo.getRepeatBin()));
		}
		if (todo.getIsImportant() != null) {
			query.addCriteria(Criteria.where("isImportant").is(todo.getIsImportant()));
		}
		List<Todo> todos = this.mongoTemplate.find(query, Todo.class);
		return new CommonResult<>(HttpStatus.HTTP_OK, "查询成功", todos);
	}

	@Override
	public CommonResult<List<Todo>> selectBetween(Date start, Date end) {
		LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String id = principal.getUser().getId();
		List<Todo> todos = todoRepository.queryTodoByTimeBetweenAndUid(start, end, id);
		return new CommonResult<>(HttpStatus.HTTP_OK, "查询成功", todos);
	}

	@Override
	public CommonResult<Todo> save(Todo todo) {
		LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		todo.setUid(principal.getUsername());
		Todo save = this.todoRepository.save(todo);
		return new CommonResult<>(HttpStatus.HTTP_OK, "更新成功", save);
	}

	@Override
	public CommonResult<String> delete(String id) {
		this.todoRepository.deleteById(id);
		LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Todo todo = this.todoRepository.queryTodoById(id);
		System.out.println(todo + " " + todo);
		if (todo != null && todo.getUid().equals(principal.getUser().getId())) {
			return new CommonResult<>(HttpStatus.HTTP_OK, "删除成功");
		} else {
			return new CommonResult<>(HttpStatus.HTTP_INTERNAL_ERROR, "无法删除非本人的记录");
		}
	}
}
